<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>新增题目</title>
        <meta name="description" content="Charcoal is a free Bootstrap 4 UI kit build by @attacomsian at Wired Dots." />
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <!--Bootstrap 4-->
        <link rel="stylesheet" href="css/bootstrap.min.css">
        <!-- <link rel="stylesheet" href="css/common.css"> -->
    </head>
    <body>
        
        
        <nav class="navbar navbar-expand-md navbar-dark fixed-top sticky-navigation">
            <a class="navbar-brand font-weight-bold" href="#">在线oj系统</a>
        

            <div class="collapse navbar-collapse" id="topMenu">
                <ul class="navbar-nav ml-auto">
                    <li class="nav-item active">
                        <a class="nav-link page-scroll" href="login.html">登录 <span class="sr-only">(current)</span></a>
                    </li>              
            </div>
        </nav>

        <!--hero section-->
        <section class="bg-hero">
            <div class="container">
                <div class="row vh-100">
                    <div class="col-sm-12 my-auto text-center">
                        <h1>在线oj系统</h1>
                        <p class="lead text-capitalize my-4">
                            基于java Servlet 实现的oj系统
                        </p>
                        <a href="https://gitee.com" class="btn btn-outline-light btn-radius btn-lg">项目连接</a>
                    </div>
                </div>
            </div>
        </section>

        <!--components-->
        <section class="my-5 pt-5">
            <div class="container">
                
                    <div class="row mt-4">
                        <div class="col-sm-12 pb-4">
                            <div class="form-group">
                                <label for="title">请输入题目名称</label>
                                <input type="text" class="form-control" id="title" placeholder="题目名称">
                           </div>                        
                        </div>
                    </div>
    
                    <div class="row mt-4">
                        <div class="col-sm-12 pb-4">
                            <div class="form-group">                        
                                <label for="level">请输入题目难度</label>
                                <input type="text" class="form-control" id="level" placeholder="题目难度">                         
                           </div>                        
                        </div>
                    </div>
    
                    <div class="row mt-4">
                        <div class="col-sm-12 pb-4">
                            <div class="form-group">                               
                                <label for="description">请输入题目详情</label>
                                <textarea class="form-control" id="description" rows="10"></textarea>
                           </div>                        
                        </div>
                    </div>
    
                    <div class="row mt-4">
                        <div class="col-sm-12 pb-4">
                            <div class="form-group">
                                <label for="codeEditor">请输入代码模板</label>
                                <div id="editortmp" style="min-height: 400px">
                                   <textarea class="form-control" id="codeEditor" style="width: 100%; height: 400px;"></textarea>
                                </div>
                           </div>                        
                        </div>
                    </div>
    
                    <div class="row mt-4">
                        <div class="col-sm-12 pb-4">
                            <div class="form-group">
                                <label for="codeEditor">请输入测试用例</label>
                                <div id="editortest" style="min-height: 400px">
                                    <textarea class="form-control" id="codeEditor" style="width: 100%; height: 400px;"></textarea>
                                </div>
    
                                <button type="button" class="btn btn-success" id="submitButton">提交按钮</button>
                           </div>                        
                        </div>
                    </div>
                
                
            </div>      
        </section>

        
        <!--footer-->
        <section class="py-5 bg-dark">
            <div class="container">
                <div class="row">
                    <div class="col-md-6 offset-md-3 col-sm-8 offset-sm-2 col-xs-12 text-center">
                        <!-- <h3>Upgrade to Pro Version</h3>
                        <p class="pt-2">
                            We are working on <b>Charcoal Pro</b> which will be released soon. The pro version 
                            will have a lot more components, sections, icons, plugins and example pages. 
                            Join the waiting list to get notified when we release it (plus discount code).
                        </p>
                        <a class="btn btn-warning" href="https://wireddots.com/newsletter">Join Waiting List</a>
                        <hr class="my-5"/> -->
                        <p class="pt-2 text-muted">
                            &copy;  by 谢超帮
                            

                    </div>
                </div>
            </div>
        </section>

        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.3/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta.2/js/bootstrap.min.js"></script>
        <script src="js/app.js"></script>

        <!-- 代码编辑ace.js  本地-->
        <script src="https://cdn.bootcdn.net/ajax/libs/ace/1.2.9/ace.js"></script>
        <script src="https://cdn.bootcdn.net/ajax/libs/ace/1.2.9/ext-language_tools.js"></script>
    
        <script>
        
            function initAcetmp(){
                let editortmp = ace.edit("editortmp");
                editortmp.setOptions({
                    enableBasicAutocompletion:true,
                    enableSnippets:true,
                    enableLiveAutocompletion:true
                });
                editortmp.setTheme("ace/theme/twilight");
                editortmp.session.setMode("ace/mode/java");
                editortmp.resize();
                document.getElementById('editortmp').style.fontSize='20px';

                return editortmp;
            }
           let editortmp =  initAcetmp();
           function initAcetest(){
                let editortest = ace.edit("editortest");
                editortest.setOptions({
                    enableBasicAutocompletion:true,
                    enableSnippets:true,
                    enableLiveAutocompletion:true
                });
                editortest.setTheme("ace/theme/twilight");
                editortest.session.setMode("ace/mode/java");
                editortest.resize();
                document.getElementById('editortest').style.fontSize='20px';

                return editortest;
            }
            let editortest =  initAcetest();


            console.log(location.search);//得到当前页面的queryString

            //设置全局ajax处理逻辑
            $.ajaxSetup({
                //设置ajax请求结束后的执行动作
                complete: function (xhr) {
                    // 通过XMLHttpRequest取得响应头，sessionstatus
                    if("REDIRECT" == xhr.getResponseHeader("REDIRECT")){ //若HEADER中含有REDIRECT说明后端想重定向，
                        var win = window;
                        while(win != win.top){
                            win = win.top;
                        }
                        win.location.href = xhr.getResponseHeader("CONTENTPATH");//将后端重定向的地址取出来,使用win.location.href去实现重定向的要求
                    }
                }
            });
            let title = document.querySelector("#title");


            console.log(title.value);
                
                //3.给提交按钮一个点击事件
                let submitButton = document.querySelector("#submitButton");
                
                submitButton.onclick = function(){ 
                    if(jQuery.trim(jQuery("#title").val()) == ""){
                        alert("输入信息不完整请重新输入!");
                        return;
                    }
                    if(jQuery.trim(jQuery("#level").val()) == ""){
                        alert("输入信息不完整请重新输入!");
                        return;
                    }
                    if(jQuery.trim(jQuery("#description").val()) == ""){
                        alert("输入信息不完整请重新输入!");
                        return;
                    }
                    if(jQuery.trim(editortmp.getValue()) == ""){
                        alert("输入信息不完整请重新输入!");
                        return;
                    }
                    if(jQuery.trim(editortest.getValue()) == ""){
                        alert("输入信息不完整请重新输入!");
                        return;
                    }
                    let body={
                        title:jQuery("#title").val(),
                        level:jQuery("#level").val(),
                        description:jQuery("#description").val(),
                        templateCode:editortmp.getValue(),
                        testCode:editortest.getValue(),
                    };


                    jQuery.ajax({
                        url:"addproblem",
                        type:"post",
                        contentType : "application/json",
                        data:JSON.stringify(body),
                        success:function(data){
                            location.assign("/adminList.html")
                        }
                    })



                }
         

            
        </script>
    </body>
</html>
